<?php

class FilterHiveTable extends HiveCliActivity {
	private $input;
	private $output;
	
	public function __construct($activity) {
		parent::__construct($activity);
		$this->input = $this->activity['inputs'][0];
		$this->output = $this->activity['outputs'][0];
	}
	
	protected function prepareInputs() {
		// Because we are in hive action context, we load input physical resource as HIVE		
    	$physicalResource = $this->resourceService->getResourcePhysicalByType($this->input['resource']['res_id'], PhysicalType::Hive);
        if(!isset($physicalResource)) {
            Yii::log("can't find physical as hive for resource[{$this->input['resource']['res_name']}]", CLogger::LEVEL_ERROR, WORKER_LOG);
            $this->setRuntimeInfo(array(
				'state'	=>	TaskState::Failed,
				'message'	=>	"[FilterHiveTable/prepare]can't find physical as hive for resource[{$this->input['resource']['res_name']}]",
			));
            return false;
        }
        $this->input['resource']['physical'] = $physicalResource;
        return true;
	}
	
	protected function prepareOutputs() {
		if(false === $this->resourceService->editResourceAttributes($this->output['resource']['res_id'], array('job_id'=>$this->env['JOBID']))) {
			 $this->setRuntimeInfo(array(
				'state'	=>	TaskState::Failed,
				'message'	=>	"edit output resource [{$this->output['resource']['res_name']}] job_id failed",
			));
			return false;
		}
		// Create output physical resouce as HIVE 
		$outputPhysical = array(
			'physical_name'	=>	"{$this->output['resource']['res_name']}_hive",
			'res_id'		=>	$this->output['resource']['res_id'],
			'storage'		=>	$this->output['resource']['res_name'],
	        'physical_type'	=>	PhysicalType::Hive,
			'partition_format'=> 'Ymd/Hi',
	        'system_id'		=>	$this->input['resource']['physical']['system_id'],
	        'keep_days'		=>	5,
        );
        
        $physicalId = $this->resourceService->addResourcePhysical($outputPhysical);
	 	if($physicalId === false) {
            Yii::log("add output physical as hive for resource[{$this->output['resource']['res_name']}] failed", CLogger::LEVEL_ERROR, WORKER_LOG);
            $this->setRuntimeInfo(array(
				'state'	=>	TaskState::Failed,
				'message'	=>	"[FilterHiveTable/prepare]add output physical as hive for resource[{$this->output['resource']['res_name']}] failed",
			));
            return false;
        }
        $outputPhysical['physical_id'] = $physicalId;
		$this->output['resource']['physical'] = $outputPhysical;
		return true;
	}
	
	protected function preparePrograms() {
		$programs = CJSON::decode($this->activity['programs']);			
    	$programs[0] = $this->concreteProgram($programs[0], array(
    		'${sourceTable}'	=>	$this->input['resource']['physical']['storage'],
    		'${destTable}'		=>	$this->output['resource']['physical']['storage'],
    		'${columns}'		=>	$this->generateSelect($this->input['filter']['select']),
    		'${condition}'		=>	$this->input['filter']['condition'],
    	));
    	return $programs;
	}
	
	protected function save() {		
		if(!parent::save()) {
			return false;
		}
		
		Yii::log("save output resource:[{$this->output['resource']['res_name']}]", CLogger::LEVEL_INFO, WORKER_LOG);
		
		// Save the resource partition.
		$partitionRecord = array(
			'physical_id'	=>	$this->output['resource']['physical']['physical_id'],
			'partition'		=>	SpecialPartition::None,
			'size'			=>	$this->getResultSize(),
			'count'			=>	$this->getResultCount(),
			'schema'		=>	'',
			'create_time'	=>	date('Y-m-d H:i:s'),
		); 
		
		$partitionId = $this->resourceService->addResourcePartition($partitionRecord);
		if($partitionId === false) {
			Yii::log("add resource[{$this->output['resource']['res_name']}] partition failed", CLogger::LEVEL_ERROR, WORKER_LOG);
			$this->setRuntimeInfo(array(
				'state'	=>	TaskState::Failed,
				'message'	=>	"[FilterHiveTable/save]add resource[{$this->output['resource']['res_name']}] partition failed",
			));
			return false;
		}
		Yii::log("add resource[{$this->output['resource']['res_name']}] partition successfully", CLogger::LEVEL_INFO, WORKER_LOG);
		return true;
    }
}
